﻿var i; //interval on global scope
var t; //timeout on global scope
var start = function () {
    i = setInterval(function () { console.log(Date.now()); }, 3000);

};

var clearTI = function () {
    clearInterval(i);
};

var startTimeOut = function () {
    t = setTimeout(function () { startTimeOut(); console.log("timeout!"); }, 3000);
};
var clearTO = function () {
    clearTimeout(t);

};

//Wrapper
var chatApp = {};
//Array 
chatApp.messages = [];

//constructor for messages
chatApp.Messages = function (name, message) {
    this.name = name;
    this.message = message;
    //this.time = Date.now();
};
//prototype for Messages
chatApp.Messages.prototype.hasBeenSent = false;

chatApp.drawChatArea = function () {
    var h = "<div class='container'>"
        for (var i in chatApp.messages) {
            var currentMessage = chatApp.messages[i];
            h += "<div class='col-md-4'>";
            h += "<h1>";
            h += "<i class='" + currentMessage.message + "'></i> ";
            h += currentMessage.name + "</h1><br/>";
        //    h += "<img src='" + currentMessage.time + "' class='img-responsive img-thumbnail gremlin-time'/><br/>";
            h += "<div class='btn btn-danger fa fa-recycle' onclick='chatApp.scrapGremlins(" + i + ")'></div>";
            h += "<div class='btn btn-warning fa fa-edit' onclick='chatApp.showEdit(" + i + ")'></div>"
            h += "</div>";
        }
    h += "</div>";
    h += "</div>";

            document.getElementById("bottom").innerHTML = h;
}
//Add message to Array
chatApp.addMessages = function () {
    "use strict";
    var name, message;
    name = document.getElementById("name");
    message = document.getElementById("message");
   // time = document.getElementById("time");
    var s = new chatApp.Messages(name.value, message.value);
    chatApp.messages.push(s);
   // time.value = "";
    name.value = "";
    chatApp.drawChatArea();
    chatApp.myPost(s);
};
//Delete Message
chatApp.scrapMessages = function (index) {
    chatApp.messages.splice(index, 1);
    chatApp.drawChatArea();
};
/************************* AJAX ***********************/
chatApp.myPost = function (messages) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "https://gremlinapp.firebaseio.com/.json", true);
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
           alert(this.response);
           // setTimeout(myPost, 3000);
        }
     //   else {
     //       console.log(" Error" + this.status + ":" + this.response);
     //   }
    };
    xhr.onerror = function () {
        alert("Comm Error...resending in 15s");
       // setTimeout(myPost, 15000);
    };
    xhr.send(JSON.stringify(messages));

};

chatApp.myGet = function (message) {

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://brilliant-fire-2547.firebaseio.com/.json", true);
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            console.log(this.response);
            setTimeout(myPost, 3000);
        }
        else {
            console.log(this.response);
        }
        var data = JSON.parse(this.response);
        for (var x in data) {
            data[x].__proto__ = chatApp.messages.prototype;
            data[x].key = x;
            chatApp.messages.push(data[x]);
        }
        chatApp.drawChatArea();
    };
    xhr.onerror = function () {
        console.log("Comm Error...resending in 15s");
        setTimeout(myPost, 15000);
    };
    xhr.send(JSON.stringify(message));
}
/************************* AJAX ***********************/

//Clear All
chatApp.clearAll = function () {
    chatApp.messages = [];
    chatApp.drawChatArea();
    
};
chatApp.myGet();